import commonTools from "@/utils/common-tools.js";
uni.addInterceptor('request', {
	// 请求拦截
	invoke(args) {
		if(!args.data.showLoading){
			uni.showLoading({
				title: '加载中...',
				mask:true
			}) 
		} 
		// 请求拼接上token、channel、requestId等数据
		args.url = commonTools.getApiUrlPinjieTokenAppidRequestid(args.url)
		// console.log(args) 
	},
	// 响应拦截
	success(args) {
		// 判断token是否过期
		if(args.data.head&&args.data.head.errorCode=="00002"){
			uni.redirectTo({
				url: "/pages/index/index"
			}) 
			return
		}  
	},
	// 失败拦截
	fail(err) {
		console.log('interceptor-fail', err)
		uni.showToast({
			title: '数据请求失败',
			icon: 'error',
			duration: 2000
		});
	},
	// 必执行拦截
	complete(res) {
       uni.hideLoading()
	}
})
// 响应数据返回
uni.addInterceptor({
	returnValue(res) {
		if (!(!!res && (typeof res === "object" || typeof res === "function") && typeof res.then ===
			"function")) {
			return res;
		}
		return new Promise((resolve, reject) => {
			res.then((res) => res[0] ? reject(res[0]) : resolve(res[1]));
		});
	},
});